package com.example.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * <p> AOP </p>
 *
 * @author Chris
 * @since 2022/10/25 22:51
 */
@Component
@Aspect
public class LogAop {
    /**
     * 使用 slf4j 日志框架
     */
    private final static Logger log = LoggerFactory.getLogger(LogAop.class);

    @Pointcut("execution(* com.example.business.service.*.*(..))")
    public void pointcut() {

    }

    @Before("pointcut()")
    public void beforeMethod(JoinPoint joinPoint) {
        log.debug("前置通知：{}", joinPoint);
    }

    @After("pointcut()")
    public void afterMethod(JoinPoint joinPoint) {
        log.debug("后置通知：{}", joinPoint);
    }

    @Around("pointcut()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        log.debug("环绕通知-前：{}", joinPoint);
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable e) {
            log.error("error=", e);
        }
        log.debug("环绕通知-后：{}", joinPoint);
        return proceed;
    }

}